<template>
  <el-dialog
    :title="'分配专家'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="项目名称" prop="projectName">
      <el-tag size="medium">
        {{dataForm.projectName}}
      </el-tag>
    </el-form-item>
    <el-form-item label="项目简介" prop="projectDesc">
      <el-tag size="medium">
        {{dataForm.projectName}}
      </el-tag>
    </el-form-item>
    <el-form-item label="项目亮点" prop="projectHighlight">
      <el-tag size="medium">
        {{dataForm.projectName}}
      </el-tag>
    </el-form-item>
    <el-form-item label="项目时间" prop="projectTime">
      <el-tag size="medium">
        {{dataForm.projectName}}
      </el-tag>
    </el-form-item>
    <el-form-item label="申请奖项" prop="prizeId">
      <el-select v-model="dataForm.prizeId" filterable placeholder="请选择" disabled>
        <el-option
          v-for="item in prizes"
          :key="item.id"
          :label="item.prizeName"
          :value="item.id">
        </el-option>
		  </el-select>
    </el-form-item>
    <el-form-item label="评审专家" prop="experts">
      <el-checkbox-group v-model="dataForm.experts">
        <el-checkbox v-for="user in userList" :key="user.userId" :label="user.userId">{{ user.username }}</el-checkbox>
      </el-checkbox-group>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        prizes: [],
        userList: [],
        dataForm: {
          id: 0,
          projectName: '',
          projectDesc: '',
          projectHighlight: '',
          projectTime: '',
          companyId: '',
          userId: '',
          prizeId: '',
          createTime: '',
          state: '',
          file: '',
          experts: [],
          mark: ''
        },
        dataRule: {
          experts: [
            { required: true, message: '请选择一个参与打分专家', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id, userList, prizes) {
        this.prizes = prizes
        this.userList = userList
        this.dataForm.experts = userList
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/generator/project/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.projectName = data.project.projectName
                this.dataForm.projectDesc = data.project.projectDesc
                this.dataForm.projectHighlight = data.project.projectHighlight
                this.dataForm.projectTime = data.project.projectTime
                this.dataForm.prizeId = data.project.prizeId
                this.dataForm.createTime = data.project.createTime
                this.dataForm.state = data.project.state
                this.dataForm.file = data.project.file
                this.dataForm.experts = userList
                this.dataForm.mark = data.project.mark
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            console.log('this.dataForm.experts == ')
            console.log(this.dataForm.experts)
            let expertStr = ''
            for (let i = 0; i < this.dataForm.experts.length; i++) {
              if ( i == this.dataForm.experts.length - 1) {
                expertStr += this.dataForm.experts[i]
              } else {
                expertStr += this.dataForm.experts[i] + ','
              }
            }
            console.log('expertStr == ')
            console.log(expertStr)
            this.$http({
              url: this.$http.adornUrl(`/generator/project/assignExperts`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'experts': expertStr
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
